<?php
// +----------------------------------------------------------------------
// | 商标订单模型
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2016-1-18 下午6:30:12
// +----------------------------------------------------------------------
defined('BASEPATH') OR exit('No direct script access allowed');
class Trademark_order_model extends CI_Model{
	
	private $table_name;
	public function __construct(){
		parent::__construct();
		$this->table_name = 'trademark_order';
	}
	
	/**
	 * 执行wher条件
	 * @param unknown_type $where
	 * @author Baip
	 */
	private function _where($where){
		if ( is_array( $where ) ){
			foreach ( $where as $key => $val ){
				if ( is_array( $val ) ){
					$this->db->where_in($key, $val);
					unset($where[$key]);
				}else{
					$this->db->where($key, $val);
				}
			}
		}
		return true;
	}
	
	/**
	 * 添加数据
	 */
	public function addData($trademark_info){
		if ( count($trademark_info) > 0  ){
			//获取订单值
			$data_arr = $this->setOrder($trademark_info);
			$this->db->trans_start();//开启事务
			$this->db->insert($this->table_name, $data_arr);
			$insert_id = $this->db->insert_id();
			//引入专利订单联系人模型(因为CI不能直接引继承另一个模型，只能用下面方法获取另一个数据模型)
			//$this->load->model('Trademark_order_contacts_model', 'contact');
			// 			$ci = &get_instance();
			// 			$ci->contact->addData($insert_id);
			$contacts_sql = $this->bindContactsSql($insert_id);
			$this->db->query($contacts_sql);
			$this->db->trans_complete();//结束事务
			if ( $this->db->trans_status() ){
				return $insert_id;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	/**
	 * 获取订单post值并组装为数据
	 * @param unknown_type $trademark_info
	 */
	private function setOrder($trademark_info){
		$trademark_name  = $this->input->post('trademark_name',0);
		$trademark_class = $this->input->post('trademark_class',0);
		if ( empty( $trademark_name ) ){
			ajaxreturn(0 , '请填写商标名称');
		}
		if ( empty( $trademark_class ) ){
			ajaxreturn(0 , '请填写商标分类');
		}
		
		$data_arr['order_id']      = date('YmdHis');
		$data_arr['trademark_id']  = $trademark_info->id;
		$data_arr['service_price'] = $trademark_info->service_price;
		$data_arr['officer_price'] = $trademark_info->officer_price;
		$data_arr['total_price']   = $trademark_info->service_price + $trademark_info->officer_price;
		$data_arr['ontime']        = time();
		$data_arr['trademark_apply_title'] = $trademark_name;
		$data_arr['trademark_apply_class'] = $trademark_class;
		return $data_arr;
	}	
	/**
	 * 拼装订单联系人SQL语句
	 * @param unknown_type $insert_id
	 * @author Baip
	 */
	private function bindContactsSql($insert_id){
		$post_contacts_data = $this->postContactsData();
		$post_contacts_data['pid'] = $insert_id;
		//$tempKey和$tempValue两个数组临时存储插入的数据
		$temp_key_string = "";
		$temp_value_string = "";
		//遍历取出关联数组中的数据并将其存储在字符串中
		foreach($post_contacts_data as $key=>$value)
		{
			$temp_key_string .= "`".$key."`,";
			$temp_value_string .= "'".$value."',";
		}
		//除去末尾的逗号
		$temp_key_string = substr($temp_key_string,0,-1);
		$temp_value_string = substr($temp_value_string,0,-1);
		$sql = "INSERT INTO ".$this->db->dbprefix('trademark_order_contacts')."(".$temp_key_string.")  VALUES (".$temp_value_string.")";
		return $sql;
	}
	
	/**
	 * post接收值
	 */
	private function postContactsData(){
		$real_name = $this->input->post('real_name',true);//称呼
		$mobile    = $this->input->post('mobile',true);//手机号
		$email     = $this->input->post('email',true);//邮箱
		$qq        = $this->input->post('qq',true);//qq
		$weixin    = $this->input->post('weixin',true);//微信号
		if ( empty( $real_name ) || !isset($real_name) ){
			ajaxreturn(0, '请填写联系人称呼');
		}
		if( !check_mobile($mobile) ){
			ajaxreturn(0,'手机号非法,请重新填写');
		}
		//产生随机字符串
		$data_arr['real_name'] = $real_name;
		$data_arr['mobile'] = $mobile;
		$data_arr['email'] = $email;
		$data_arr['qq'] = $qq;
		$data_arr['weixin'] = $weixin;
		return $data_arr;
	}
	
	/**
	 * 获取总数
	 * @param unknown_type $where
	 * @param unknown_type $field
	 * @author Baip
	 */
	public function getCount($where, $field="*"){
		$this->_where($where);
		$this->db->select($field);
		$count = $this->db->count_all_results($this->table_name);
		return $count;
	}
	
	/**
	 * 获取多条数据
	 * @param array $where
	 * @param string $field
	 * @param string/int $limit
	 * @param int $offset
	 * @param string $order
	 */
	public function getDataObj($where, $field="*", $limit=15, $offset=0,  $order="id DESC"){
		$this->_where($where);
		$this->db->select($field);
		$this->db->order_by($order);
		$this->db->limit($limit, $offset);
		$res = $this->db->get($this->table_name);
		return $res->result();
	}
	
	/**
	 * 更新数据
	 * @param unknown_type $where
	 */
	public function updateData($where){
		if ( empty( $where ) ){
			return false;
		}
		$data_arr = $this->postOrderStatusData();
		$query = $this->db->where($where)->update($this->table_name,$data_arr);
		return $query;
	}
	
	/**
	 * 更新订单状态
	 */
	private function postOrderStatusData(){
		$status = $this->input->post('status', 0);
		$instro = $this->input->post('instro', 0);
		if ( $status > 0 ){
			$data['status'] = $status;
		}
		$data['instro'] = $instro;
		return $data;
	}
	
	/**
	 * 获取单条数据
	 * @param unknown_type $where
	 * @param unknown_type $field
	 * @param unknown_type $order
	 * @param unknown_type $limit
	 * @author Baip
	 */
	public function getFind($where, $field="*", $order="id DESC", $limit=1 ){
		//if ( empty( $where ) ) return false;
		$this->_where($where);
		$this->db->from($this->table_name);
		$this->db->select($field);
		$this->db->limit($limit);
		$this->db->order_by($order);
		$res = $this->db->get();
		return $res->row();
	}
}